//
//  DMControllerthress.m
//  DMGME
//
//  Created by ihope99 on 15/8/27.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMAgentManageController.h"
#import "DMAgentManageCell.h"
#import "popCover.h"
#import "ChoiceBuIDView.h"
#import "CancelView.h"

#import "DMCustomViewController.h"

@interface DMAgentManageController () <ChoiceBuIDViewClickProtocol>

@end

@implementation DMAgentManageController
{
    popCover *Cover;
    ChoiceBuIDView *choiceBuIdView;
    CancelView *cancelView;
    NSMutableArray *waitPassArr;
    NSMutableArray *havePassArr;
    UITableView *tablewview;
    NSString * queLiuId;
    int rowQ;
    int sectionQs ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    waitPassArr = [NSMutableArray array];
    havePassArr = [NSMutableArray array];
    self.title = @"代理商管理";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // 创建tableView
    [self createTableView];
    
    //创建表头
    
    [self createHeaderView];
    
    //请求数据
    [self StartLoading];
    [self getManageList];
}

- (void)getManageList
{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue: @"index" forKey:@"action"];
    
    [AFDownManager POST:DeleManegeNET parameters:dict success:^(id responseObject) {
//       NSLog(@"responseObjectNET == %@",responseObject);
        [self StopLoading];
        if ([responseObject[@"data"][@"havePass"] count] == 0) {
            havePassArr = nil;
        }else{
            havePassArr = responseObject[@"data"][@"havePass"];
        }
        if ([responseObject[@"data"][@"waitPass"] count] == 0) {
            waitPassArr = nil;
        }else{
            waitPassArr = responseObject[@"data"][@"waitPass"];
        }
        [tablewview reloadData];
        
//        NSLog(@"%@",responseObject);
//        NSLog(@"%@",responseObject[@"desc"]);
        
    } failure:^(NSError *error) {
    }];
}

- (void)createHeaderView{
    
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, KWidth, 75);
    headerView.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *bossInfoLable = [[UILabel alloc] init];
    bossInfoLable.frame = CGRectMake(15, 15, KWidth - 150, 13);
    bossInfoLable.font = [UIFont systemFontOfSize:13];
    bossInfoLable.textAlignment = 0;
    bossInfoLable.text = @"您的上级代理商信息:";
    
    UILabel *nameLable = [[UILabel alloc] init];
    nameLable.frame = CGRectMake(15, CGRectGetMaxY(bossInfoLable.frame)+15, 100, 13);
    nameLable.font = [UIFont systemFontOfSize:13];
    nameLable.textAlignment = 0;
    if ([kkUserDict[@"level"] isEqual:@"1"]) {
         nameLable.text = [NSString stringWithFormat:@"姓名:"];
    }else{
    nameLable.text = [NSString stringWithFormat:@"姓名:%@",kkUserDict[@"parentAgent"][@"realname"]];
    }
    
    UILabel *phoneLable = [[UILabel alloc] init];
    phoneLable.frame = CGRectMake(CGRectGetMaxX(nameLable.frame)+5, Y(nameLable), 150, 13);
    phoneLable.font = [UIFont systemFontOfSize:13];
    phoneLable.textAlignment = 0;
    
    if ([kkUserDict[@"level"] isEqual:@"1"]) {
        phoneLable.text = [NSString stringWithFormat:@"手机号:"];
    }else{
        phoneLable.text = [NSString stringWithFormat:@"手机号:%@",kkUserDict[@"parentAgent"][@"mobile"]];
    }
    
    UIButton *wordsLable = [UIButton buttonWithType:0];
    wordsLable.frame = CGRectMake(KWidth - 15 - 55, Y(nameLable), 55, 22);
    wordsLable.layer.borderWidth = 0.8;
    wordsLable.layer.borderColor = [[UIColor redColor] CGColor];
    [wordsLable setTitle:@"留言" forState:UIControlStateNormal];
    [wordsLable setTitleColor:[UIColor redColor] forState:0];
    wordsLable.titleLabel.font = [UIFont systemFontOfSize:13];
    [wordsLable addTarget:self action:@selector(liuYanBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayColor];
    lineView.frame = CGRectMake(0, 74, KWidth, 1);
    lineView.alpha = 0.2;
    
    [headerView addSubview:bossInfoLable];
    [headerView addSubview:nameLable];
    [headerView addSubview:phoneLable];
    [headerView addSubview:wordsLable];
    [headerView addSubview:lineView];

    
    [self.view addSubview:headerView];
    
}
//留言Button
-(void)liuYanBtn:(UIButton *)sender
{
    DMCustomViewController *chat = [[DMCustomViewController alloc] init];
    
    chat.targetId = kkUserDict[@"parentAgent"][@"mobile"];
    chat.userName = kkUserDict[@"parentAgent"][@"realname"];
    chat.titleStr = kkUserDict[@"parentAgent"][@"realname"];

    [self.navigationController pushViewController:chat animated:YES];
    
}
- (void)createTableView
{
    tablewview = [[UITableView alloc] initWithFrame:CGRectMake(0, 75, KWidth, KHeight -75 ) style:UITableViewStylePlain];
    tablewview.backgroundColor = [UIColor whiteColor];
    tablewview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    tablewview.delegate = self;
    tablewview.dataSource = self;
    [self.view addSubview:tablewview];
    
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor clearColor];
    footView.frame = CGRectMake(0, 0, KWidth, 10);
    [tablewview setTableFooterView:footView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] init];
    footView.frame = CGRectMake(0, 0, KWidth, 50);
    
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    lineView.alpha = 0.3;
    lineView.frame = CGRectMake(0, 0, KWidth, 15);
    
    UILabel *stateLable = [[UILabel alloc] init];
    stateLable.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), KWidth, 35);
    stateLable.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
    stateLable.layer.borderWidth = 0.5;
    stateLable.backgroundColor = [UIColor whiteColor];
    stateLable.font = [UIFont systemFontOfSize:13];
    stateLable.text = @"    待审核的经销商";
    
    [footView addSubview:lineView];
    [footView addSubview:stateLable];
    if (section == 0)
    {
    stateLable.text = @"    待审核的经销商";
        
    return footView;
    }else
    stateLable.text = @"    已被审核的经销商";
        
    return footView;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return waitPassArr.count;
    }else{
        return havePassArr.count;
    }
}
//13699296027
//18734882161
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"Cell";
    DMAgentManageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[DMAgentManageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
    }
    
    
    if (indexPath.section == 0) {
        cell.nameLable.text = waitPassArr[indexPath.row][@"realname"];
        cell.phoneLable.text = [NSString stringWithFormat:@"手机号:%@",waitPassArr[indexPath.row][@"mobile"]];
        [cell.cancelBtn setTitle:@"删除" forState:UIControlStateNormal];
        [cell.cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cell.cancelBtn addTarget:self action:@selector(cancelBtnClike:) forControlEvents:UIControlEventTouchUpInside];
        cell.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        cell.cancelBtn.layer.borderWidth = 0.8f;
        cell.cancelBtn.layer.borderColor = [[UIColor redColor] CGColor];
        [cell.queLiuBtn setTitle:@"确认身份" forState:UIControlStateNormal];
        [cell.queLiuBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cell.queLiuBtn addTarget:self action:@selector(queLiuBtnClike:) forControlEvents:UIControlEventTouchUpInside];
        cell.queLiuBtn.layer.borderWidth = 0.8;
        cell.queLiuBtn.layer.borderColor = [[UIColor redColor] CGColor];
        cell.queLiuBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        cell.cancelBtn.tag = indexPath.section *100 +indexPath.row;
        cell.queLiuBtn.tag = indexPath.section *1000 +indexPath.row;

    }else if (indexPath.section == 1){
        cell.nameLable.text = havePassArr[indexPath.row][@"realname"];
        cell.phoneLable.text = [NSString stringWithFormat:@"手机号:%@",havePassArr[indexPath.row][@"mobile"]];
//        cell.queLiuBtn.hidden = YES;
        [cell.cancelBtn setTitle:@"删除" forState:UIControlStateNormal];
        [cell.cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cell.cancelBtn addTarget:self action:@selector(cancelBtnClike:) forControlEvents:UIControlEventTouchUpInside];
        cell.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        cell.cancelBtn.layer.borderWidth = 0.8f;
        cell.cancelBtn.layer.borderColor = [[UIColor redColor] CGColor];
        [cell.queLiuBtn setTitle:@"留言" forState:UIControlStateNormal];
        [cell.queLiuBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cell.queLiuBtn addTarget:self action:@selector(yanLiuBtnClike:) forControlEvents:UIControlEventTouchUpInside];
        cell.queLiuBtn.layer.borderWidth = 0.8;
        cell.queLiuBtn.layer.borderColor = [[UIColor redColor] CGColor];
        cell.queLiuBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        cell.cancelBtn.tag = indexPath.section *100 +indexPath.row;
         cell.queLiuBtn.tag = indexPath.section *10000 +indexPath.row;
//        NSLog(@"========== %@",havePassArr[indexPath.row][@"mobile"]);
    }
        return cell;
}
-(void)yanLiuBtnClike:(UIButton *)sender
{
    DMCustomViewController *chat = [[DMCustomViewController alloc] init];
    rowQ = sender.tag%1000;
    long sectionQ = sender.tag/1000;
    if (sectionQ == 0) {
        queLiuId = waitPassArr[rowQ][@"id"];
    }else{
        chat.targetId = havePassArr[rowQ][@"mobile"];
        chat.userName = havePassArr[rowQ][@"realname"];
        chat.titleStr = havePassArr[rowQ][@"realname"];
    }

  [self.navigationController pushViewController:chat animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 删除按钮
//- (void)cancelBtnClike{
//    Cover = [popCover showInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+64)];
//    Cover.dimBackground = YES;
//
//    cancelView = [[CancelView alloc] init];
//    cancelView.frame = CGRectMake((KWidth - 260)/2, (KHeight - 64 - 120)/2, 260, 120);
//    cancelView.mDelegate = self;
//    cancelView.OnBtnClick = @selector(OnBtnClick:);
//    cancelView.OnBtnClickTwo = @selector(OnBtnClickTwo:);
//    cancelView.backgroundColor = [UIColor whiteColor];
//    NSLog(@"===%ld",cancelView.btn.tag);
//    [Cover addSubview:cancelView];
//}

#pragma mark - 消除蒙版图
- (void)xiaochuMengBanTu{
    for (UIView *veiw in Cover.subviews) {
        if ([veiw isKindOfClass:[UIView class]]) {
            [UIView animateWithDuration:1 animations:^{
                //                choiceBuIdView.alpha = 0;
                [veiw removeFromSuperview];
            } completion:^(BOOL finished) {
                
            }];
        }
    }
     [Cover dismissCover];
}
//- (void)OnBtnClick:(id)sender
//{
//    [self xiaochuMengBanTu];
//    
////    [self showMsg:@"还没接口"];
//}
//- (void)OnBtnClickTwo:(id)sender
//{
//    [self xiaochuMengBanTu];
//}

#pragma mark - 确认身份
- (void)queLiuBtnClike:(UIButton *)btn
{
    rowQ = btn.tag%1000;
   long sectionQ = btn.tag/1000;
    if (sectionQ == 0) {
        queLiuId = waitPassArr[rowQ][@"id"];
    }else{
        queLiuId = havePassArr[rowQ][@"id"];
    }
    
    Cover = [popCover showInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+64)];
    Cover.dimBackground = YES;
 
    choiceBuIdView = [[ChoiceBuIDView alloc] initWithFrame:CGRectMake((KWidth - 260)/2, (KHeight - 64 - 120)/2, 260, 120) withNum:[kkUserDict[@"level"] intValue]];
    choiceBuIdView.delegete = self;
    choiceBuIdView.backgroundColor = [UIColor whiteColor];
    [choiceBuIdView.cancelBtn addTarget:self action:@selector(choiceBuIdViewClick) forControlEvents:UIControlEventTouchUpInside];
    [Cover addSubview:choiceBuIdView];
}
- (void)choiceBuIdViewClick{
    [self xiaochuMengBanTu];
}


- (void)cancelBtnClike:(UIButton *)btn{
    
    NSString * delete_id;
    int row = btn.tag%100;
   long int section = btn.tag/100;
    
//    NSLog(@"%d %ld",row,section);
    
    if (section == 0) {
        delete_id = waitPassArr[row][@"id"];
    }else{
        delete_id = havePassArr[row][@"id"];
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$deleteAgent"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue: delete_id forKey:@"delete_id"];
    [dict setValue: @"deleteAgent" forKey:@"action"];
    [self StartLoading];
    [AFDownManager POST:DeleManegeNET parameters:dict success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] intValue] == 0 ) {
            [tablewview reloadData];
            [self getManageList];
        }
        
//        NSLog(@"%@",responseObject[@"desc"]);
        
    } failure:^(NSError *error) {
    }];
    
    
}

#pragma mark - cancelView 的代理方法
-(void)prensetChoiceBuIDViewClick:(NSInteger)sender
{
    NSString * levle;
//    NSLog(@"%ld",(long)sender);
    if (sender == 20){
        if ([kkUserDict[@"level"] isEqual:@"1"]) {
            levle = @"2";
        }else if ([kkUserDict[@"level"] isEqual:@"2"]){
            levle = @"3";
        }else if ([kkUserDict[@"level"] isEqual:@"3"]){
            levle = @"4";
        }
    }else if (sender == 21){
        if ([kkUserDict[@"level"] isEqual:@"1"]) {
            levle = @"3";
        }else if ([kkUserDict[@"level"] isEqual:@"2"]){
            levle = @"4";
        }
    }else{
        if ([kkUserDict[@"level"] isEqual:@"1"]) {
            levle = @"4";
        }

    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$passIndentity"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue: kkUserDict[@"level"] forKey:@"level"];
    [dict setValue: levle forKey:@"pass_level"];
    [dict setValue: queLiuId forKey:@"pass_id"];
    [dict setValue: @"passIndentity" forKey:@"action"];
//    NSLog(@"%@",dict);
    
    [self StartLoading];
    
    [AFDownManager POST:DeleManegeNET parameters:dict success:^(id responseObject) {
//        NSLog(@"----%@",responseObject);
        if ([responseObject[@"code"] intValue] == 0 ) {
//            [waitPassArr removeAllObjects];
//            [havePassArr removeAllObjects];
//            [tablewview reloadData];
            [self getManageList];
        }
        
//        NSLog(@"%@",responseObject[@"desc"]);
        
    } failure:^(NSError *error) {
    }];
     [self xiaochuMengBanTu];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
